package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commen.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    //添加套餐
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetMeal(setmealDto);
        return R.success("保存成功");
    }
    
    //获取分页对象
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);

        setmealService.page(setmealPage,queryWrapper);

        //复制分页信息
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        //显示套餐分类
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = new ArrayList<>();

        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(record,setmealDto);

            Category category = categoryService.getById(record.getCategoryId());
            if (category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            setmealDtoList.add(setmealDto);
        }

        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }

    //获取启售的商品



    //删除套餐
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)  //删除所有setmealCache
    public R<String> deleteSetmeal(@RequestParam List<Long> ids){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        //检查里面是否有起售的套餐，如果有就禁止删除
        if (setmealService.count(queryWrapper)>0){
            return R.error("不能删除启售中的套餐");
        }

        setmealService.removeSetMeal(ids);
        return R.success("删除成功");
    }

    //改变商品售卖状态
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<String> setmealStatusByStatus(@PathVariable(value = "status") int status, @RequestParam List<Long> ids) {
        System.out.println(status+"=========="+ids);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);

        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealService.update(setmeal,queryWrapper);
        return R.success("更改成功");
    }

    //查询套餐里面的菜品
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        System.out.println("进入setmeal");
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
    
}
